<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/12-16:09
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\controller;

    use app\wechat\service\WechatService;
    use think\Db;

    /**
     * Class Index
     * @package app\wechat\controller
     */
    class Index extends ChatBase
    {
        /**
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: index
         * @describe: 微信统计
         */
        public function index()
        {
            $map = ['appid' => WechatService::getAppid()];

            $this->totalJson = ['xs' => [], 'ys' => []];

            for ($i = 9; $i >= 0; $i--) {
                $time = strtotime("-{$i} months");
                $where = [['subscribe_at', '<=', date('Y-m-t 23:59:59', $time)]];
                $this->totalJson['xs'][] = date('Y年m月', $time);
                $item = ['_0' => 0, '_1' => 0];

                $list = Db::name('WechatFans')->field('count(1) count,is_black black')->where($map)->where($where)->group('is_black')->select();

                foreach ($list as $vo) $item["_{$vo['black']}"] = $vo['count'];

                $this->totalJson['ys']['_0'][] = $item['_0'];
                $this->totalJson['ys']['_1'][] = $item['_1'];
            }
            $this->totalFans = Db::name('WechatFans')->where(['is_black' => '0'])->where($map)->count();
            $this->totalBlack = Db::name('WechatFans')->where(['is_black' => '1'])->where($map)->count();
            $this->totalNews = Db::name('WechatNews')->where(['is_deleted' => '0'])->count();
            $this->totalRule = Db::name('WechatKeys')->count();

            return $this->fetch();
        }

    }